<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <div style="width: 100%">
              <el-radio-group v-model="queryParams.state" style="margin-bottom: 30px" @change="handleQuery">
                <el-radio-button value="2">已接单</el-radio-button>
                <el-radio-button value="1">待接单</el-radio-button>
                <el-radio-button value="4">已完成</el-radio-button>
                <el-radio-button value="3">已取消</el-radio-button>
                <el-radio-button value="0">未支付</el-radio-button>
              </el-radio-group>
            </div>

            <el-form-item label="项目" prop="title">
              <el-input v-model="queryParams.title" placeholder="请输入项目" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="下单用户ID" label-width="120" prop="userId">
              <el-input v-model="queryParams.userId" placeholder="请输入下单用户" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="达人用户ID" label-width="150" prop="darenUserId">
              <el-input v-model="queryParams.darenUserId" placeholder="请输入达人用户" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="订单号" prop="orderSn">
              <el-input v-model="queryParams.orderSn" placeholder="请输入订单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="陪玩类型" prop="typeTitle">
              <el-input v-model="queryParams.typeTitle" placeholder="请输入陪玩类型" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
<!--          <el-col :span="1.5">-->
<!--            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['app:SkYueDanOrder:add']">新增</el-button>-->
<!--          </el-col>-->
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['app:SkYueDanOrder:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['app:SkYueDanOrder:remove']">删除</el-button>
          </el-col>
<!--          <el-col :span="1.5">-->
<!--            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['app:SkYueDanOrder:export']">导出</el-button>-->
<!--          </el-col>-->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="SkYueDanOrderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" width="130" v-if="true" />
        <el-table-column label="约玩项目" align="center" prop="title" />
        <el-table-column label="陪玩类型" align="center" prop="darenTitle" width="120"/>
        <el-table-column label="陪玩单格" align="center" prop="price" />
        <el-table-column label="陪玩时长" align="center" prop="longTime" />
        <el-table-column label="陪玩总价" align="center" prop="allPrice" />
        <el-table-column label="优惠金额" align="center" prop="zhekouMoney" />
        <el-table-column label="支付金额" align="center" prop="payPrice" />
        <el-table-column label="开始时间" width="180" align="center" prop="startTime" />
        <el-table-column label="地址" width="210" align="center" prop="address" />
        <el-table-column label="备注" align="center" prop="notes" />
        <el-table-column label="支付方式" align="center" prop="payType">
          <template #default="scope">
            <el-tag v-if="scope.row.payType==0" type="primary">微信</el-tag>
            <el-tag v-else type="success">支付宝</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
        <el-table-column label="下单用户" align="center" prop="userId" />
        <el-table-column label="达人用户" align="center" prop="darenUserId" />
        <el-table-column label="订单号" align="center" prop="orderSn" width="120"/>
        <el-table-column label="支付状态" align="center" prop="payState">
          <template #default="scope">
            <el-tag v-if="scope.row.payState==0" type="primary">未支付</el-tag>
            <el-tag v-else type="success">已支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="订单状态" align="center" prop="state">
          <template #default="scope">
<!--            0:未支付  1：待接单  2：已接单 3：已取消 4：已完成-->
            <el-tag v-if="scope.row.state==0" type="info">未支付</el-tag>
            <el-tag v-else-if="scope.row.state==1" type="danger">待接单</el-tag>
            <el-tag v-else-if="scope.row.state==2" type="success">已接单</el-tag>
            <el-tag v-else-if="scope.row.state==3" type="warning">已取消</el-tag>
            <el-tag v-else-if="scope.row.state==4" type="primary">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="城市编码" align="center" prop="city" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="InfoFilled" @click="handleUpdate(scope.row)" v-hasPermi="['app:SkYueDanOrder:edit']"></el-button>
            </el-tooltip>
<!--            <el-tooltip content="删除" placement="top">-->
<!--              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['app:SkYueDanOrder:remove']"></el-button>-->
<!--            </el-tooltip>-->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>

<!--    详情对话框-->
    <el-drawer
      v-model="dialog.visible"
      title="订单详情"
      size="50%"
      direction="rtl"
      :before-close="handleClose"
    >

      <div>
        <el-descriptions title="下单用户">
          <el-descriptions-item label="用户名称">{{form.userVo.nickName}}</el-descriptions-item>
          <el-descriptions-item label="电话">{{form.userVo.phone}}</el-descriptions-item>
          <el-descriptions-item label="性别">{{form.userVo.sex}}</el-descriptions-item>
          <el-descriptions-item label="身高">{{form.userVo.height}}</el-descriptions-item>
          <el-descriptions-item label="擅长">
            <el-tag style="margin-left: 10px;" v-if="form.userVo.beGoodAt"
                    v-for="(item, index) in JSON.parse(form.userVo.beGoodAt)" type="primary">{{ item }}
            </el-tag>
         </el-descriptions-item>
          <el-descriptions-item label="个性标签">
            <el-tag style="margin-left: 10px;" v-if="form.userVo.personalityTag"
                    v-for="(item, index) in JSON.parse(form.userVo.personalityTag)" type="primary">{{ item }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <div style="margin-top: 20px">
      <el-descriptions title="订单信息">
        <el-descriptions-item label="订单号">{{form.orderSn}}</el-descriptions-item>
        <el-descriptions-item label="约玩项目">{{form.title}}</el-descriptions-item>
        <el-descriptions-item label="开始时间">{{form.startTime}}</el-descriptions-item>
        <el-descriptions-item label="约玩单价">{{form.price}}</el-descriptions-item>
        <el-descriptions-item label="约玩类型">{{form.darenTitle}}</el-descriptions-item>
        <el-descriptions-item label="陪玩时长">{{form.longTime}}</el-descriptions-item>
        <el-descriptions-item label="陪玩总价">{{form.allPrice}}</el-descriptions-item>
        <el-descriptions-item label="折扣">{{form.zhekou}}</el-descriptions-item>
        <el-descriptions-item label="折扣总价">{{form.zhekouMoney}}</el-descriptions-item>
        <el-descriptions-item label="支付金额">{{form.payPrice}}</el-descriptions-item>
        <el-descriptions-item label="地址">
          {{form.address}}
        </el-descriptions-item>
      </el-descriptions>
      </div>

      <div style="margin-top: 20px">
        <el-descriptions title="达人信息">
          <el-descriptions-item label="用户名称">{{form.drUserVo.nickName}}</el-descriptions-item>
          <el-descriptions-item label="电话">{{form.drUserVo.phone}}</el-descriptions-item>
          <el-descriptions-item label="性别">{{form.drUserVo.sex}}</el-descriptions-item>
          <el-descriptions-item label="身高">{{form.drUserVo.height}}</el-descriptions-item>
          <el-descriptions-item label="擅长">
            <el-tag style="margin-left: 10px;" v-if="form.drUserVo.beGoodAt"
                    v-for="(item, index) in JSON.parse(form.drUserVo.beGoodAt)" type="primary">{{ item }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="个性标签">
            <el-tag style="margin-left: 10px;" v-if="form.drUserVo.personalityTag"
                    v-for="(item, index) in JSON.parse(form.drUserVo.personalityTag)" type="primary">{{ item }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </div>

      <div style="margin-top: 20px">
        <el-timeline style="max-width: 600px">
          <el-timeline-item :timestamp="item.createTime" placement="top" v-for="(item,index) in form.list">
            <el-card>
              <p>{{item.nickName}}({{item.phone}})</p>
              <h4>{{item.title}}</h4>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>
    </el-drawer>
  </div>
</template>

<script setup name="SkYueDanOrder" lang="ts">
import { listSkYueDanOrder, getSkYueDanOrder, delSkYueDanOrder, addSkYueDanOrder, updateSkYueDanOrder,getNewInfo } from '@/api/app/SkYueDanOrder';
import { SkYueDanOrderVO, SkYueDanOrderQuery, SkYueDanOrderForm } from '@/api/app/SkYueDanOrder/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_city_title } = toRefs<any>(proxy?.useDict('sys_city_title'));
const SkYueDanOrderList = ref<SkYueDanOrderVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const SkYueDanOrderFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: SkYueDanOrderForm = {
  id: undefined,
  title: undefined,
  price: undefined,
  longTime: undefined,
  startTime: undefined,
  address: undefined,
  notes: undefined,
  payType: undefined,
  userId: undefined,
  darenUserId: undefined,
  orderSn: undefined,
  typeTitle: undefined,
  state:undefined,
  list:[],
  userVo:{
    nickName:undefined,
    phone:undefined
  },
  drUserVo:{
    nickName:undefined,
    phone:undefined
  }
}
const data = reactive<PageData<SkYueDanOrderForm, SkYueDanOrderQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: undefined,
    price: undefined,
    longTime: undefined,
    startTime: undefined,
    address: undefined,
    notes: undefined,
    payType: undefined,
    userId: undefined,
    darenUserId: undefined,
    orderSn: undefined,
    typeTitle: undefined,
    state:2,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "ID不能为空", trigger: "blur" }
    ],
    title: [
      { required: true, message: "项目不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "价格不能为空", trigger: "blur" }
    ],
    longTime: [
      { required: true, message: "时长不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "地址不能为空", trigger: "blur" }
    ],
    notes: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
    payType: [
      { required: true, message: "支付方式不能为空", trigger: "change" }
    ],
    userId: [
      { required: true, message: "下单用户不能为空", trigger: "blur" }
    ],
    darenUserId: [
      { required: true, message: "达人用户不能为空", trigger: "blur" }
    ],
    orderSn: [
      { required: true, message: "订单号不能为空", trigger: "blur" }
    ],
    typeTitle: [
      { required: true, message: "陪玩类型不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询约单订单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSkYueDanOrder(queryParams.value);
  SkYueDanOrderList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  SkYueDanOrderFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: SkYueDanOrderVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加约单订单";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: SkYueDanOrderVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getNewInfo(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改约单订单";
}

/** 提交按钮 */
const submitForm = () => {
  SkYueDanOrderFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateSkYueDanOrder(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addSkYueDanOrder(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: SkYueDanOrderVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除约单订单编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delSkYueDanOrder(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('app/SkYueDanOrder/export', {
    ...queryParams.value
  }, `SkYueDanOrder_${new Date().getTime()}.xlsx`)
}

/** 详情按钮操作 */
const getInfo = async (row?: SkYueDanOrderVO) => {
  const _id = row?.id || ids.value[0]
  const res = await getNewInfo(_id);
  Object.assign(form.value, res.data);
}

//关闭详情
const handleClose = () =>{
  dialog.visible = false;
}
onMounted(() => {
  getList();
});
</script>
